/*
Copyright 2023 The Nuclio Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package iotcoremqtt

import (
	"time"

	"github.com/nuclio/nuclio/pkg/functionconfig"
	"github.com/nuclio/nuclio/pkg/processor/runtime"
	"github.com/nuclio/nuclio/pkg/processor/trigger"
	"github.com/nuclio/nuclio/pkg/processor/trigger/mqtt"

	"github.com/mitchellh/mapstructure"
	"github.com/nuclio/errors"
)

type Configuration struct {
	mqtt.Configuration
	ProjectID          string
	RegionName         string
	RegistryID         string
	DeviceID           string
	PrivateKey         trigger.Secret
	JWTRefreshInterval string
	jwtRefreshInterval time.Duration
}

func NewConfiguration(id string,
	triggerConfiguration *functionconfig.Trigger,
	runtimeConfiguration *runtime.Configuration) (*Configuration, error) {
	newConfiguration := Configuration{}

	// create base
	mqttConfiguration, err := mqtt.NewConfiguration(id, triggerConfiguration, runtimeConfiguration)
	if err != nil {
		return nil, errors.Wrap(err, "Failed to read MQTT configuration")
	}

	// set in new config
	newConfiguration.Configuration = *mqttConfiguration

	// parse attributes
	if err := mapstructure.Decode(newConfiguration.Attributes, &newConfiguration); err != nil {
		return nil, errors.Wrap(err, "Failed to decode attributes")
	}

	if newConfiguration.JWTRefreshInterval == "" {
		newConfiguration.JWTRefreshInterval = "1h"
	}

	newConfiguration.jwtRefreshInterval, err = time.ParseDuration(newConfiguration.JWTRefreshInterval)
	if err != nil {
		return nil, errors.Wrap(err, "Failed to parse JWT refresh interval")
	}

	return &newConfiguration, nil
}
